package com.example.edits;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button1;
    Button button2;
    EditText editText;

    Button btn_save;

    EditText edt_name;
    EditText edt_age;
    EditText edt_addr;

    Spinner spn_hob;


    private void initView(){
        button1 = findViewById(R.id.btn1);
        button2 = findViewById(R.id.btn2);
        editText = findViewById(R.id.edt1);
        btn_save = findViewById(R.id.btn_save);

        edt_name = findViewById(R.id.edt_name);
        edt_age = findViewById(R.id.edt_age);
        edt_addr = findViewById(R.id.edt_addr);
        spn_hob = findViewById(R.id.spn_hob);

        btn_save.setOnClickListener(v->{
            String name = edt_name.getText().toString();
            String age = edt_age.getText().toString();
            String addr = edt_addr.getText().toString();
            String hob = spn_hob.getSelectedItem().toString();
            String msg = String.format("%s %s %s %s", name, age,addr,hob);
            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setText("");
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,
                                editText.getText().toString(),
                                Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       initView();

        if(savedInstanceState != null){
            edt_name.setText(savedInstanceState.getString("name"));
            edt_age.setText(savedInstanceState.getString("age"));
            edt_addr.setText(savedInstanceState.getString("addr"));
        }
    }


    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("name",edt_name.getText().toString());
        outState.putString("age",edt_age.getText().toString());
        outState.putString("addr",edt_addr.getText().toString());
        outState.putString("hobby",spn_hob.getSelectedItem().toString());
    }
}